package com.sinodata.bsm.cicp.collector.was6;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.finder.was6.Util;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:01:04   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class JdbcPoolCollecter extends WasCollector {

    private static final Logger logger = Logger.getLogger(JdbcPoolCollecter.class);
    private static int TIME_OUT = 15 * 60 * 1000;
    private static final String MODULE = "connectionPoolModule";

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> value = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        String jdbcPoolName = this.paramMap.get("jdbcPoolName");
        try {
            String port = this.getParamMap().get("ServerPort");
            String respXml = WasValueBuffer.getInstance().getXml(ip, port, MODULE);
            if (respXml == null) {
                return null;
            }
            Document doc = DocumentHelper.parseText(respXml);
            Element root = doc.getRootElement();
            Node node = root.selectSingleNode("Node[@name='" + this.paramMap.get("NodeName") + "']");
            Node server = node.selectSingleNode("Server[@name='" + this.paramMap.get("ServerName") + "']");
            List<Element> data = server.selectNodes("Stat/Stat");
            for (Element cp : data) {
                String name = cp.attributeValue("name");
                if (!name.equals(jdbcPoolName)) {
                    continue;
                }
                // 创建数
                ResPropertyValue.PropValue[] proValues = new ResPropertyValue.PropValue[getPropMeta().length];
                ResPropertyValue propertyValueX = new ResPropertyValue();
                Node e1 = cp.selectSingleNode("CountStatistic[@ID='1']");
                String CreateCount = DocumentHelper.parseText(e1.asXML()).getRootElement().attributeValue("count");
                proValues[0] = ResPropertyValue.createPropValue(CreateCount, propMetas[0]);
                // 关闭数
                Node e2 = cp.selectSingleNode("CountStatistic[@ID='2']");
                String CloseCount = DocumentHelper.parseText(e2.asXML()).getRootElement().attributeValue("count");
                proValues[1] = ResPropertyValue.createPropValue(CloseCount, propMetas[1]);
                // jdbc池大小
                Node e3 = cp.selectSingleNode("BoundedRangeStatistic[@ID='5']");
                String PoolSize = DocumentHelper.parseText(e3.asXML()).getRootElement().attributeValue("value");
                proValues[2] = ResPropertyValue.createPropValue(PoolSize, propMetas[2]);
                // 空闲大小
                Node e4 = cp.selectSingleNode("BoundedRangeStatistic[@ID='6']");
                String FreePoolSize = DocumentHelper.parseText(e4.asXML()).getRootElement().attributeValue("value");
                proValues[3] = ResPropertyValue.createPropValue(FreePoolSize, propMetas[3]);
                // 等待线程数
                Node e5 = cp.selectSingleNode("RangeStatistic[@ID='7']");
                String WaitingThreadCount = DocumentHelper.parseText(e5.asXML()).getRootElement().attributeValue("value");
                proValues[4] = ResPropertyValue.createPropValue(WaitingThreadCount, propMetas[4]);
                // 使用率
                Node e6 = cp.selectSingleNode("RangeStatistic[@ID='9']");
                String PercentUsed = DocumentHelper.parseText(e6.asXML()).getRootElement().attributeValue("value");
                proValues[5] = ResPropertyValue.createPropValue(PercentUsed, propMetas[5]);
                // 使用时间
                Node e7 = cp.selectSingleNode("TimeStatistic[@ID='12']");
                String UseTime = DocumentHelper.parseText(e7.asXML()).getRootElement().attributeValue("totalTime");
                proValues[6] = ResPropertyValue.createPropValue(UseTime, propMetas[6]);
                // 空闲时间
                Node e8 = cp.selectSingleNode("TimeStatistic[@ID='13']");
                String WaitTime = DocumentHelper.parseText(e8.asXML()).getRootElement().attributeValue("totalTime");
                proValues[7] = ResPropertyValue.createPropValue(WaitTime, propMetas[7]);
                propertyValueX.setValues(proValues);
                value.add(propertyValueX);

            }
        } catch (Exception e) {
            logger.error("failed to " + ip + " collect by JdbcPoolCollecter", e);
            throw new CollectorException(e);
        }

        return value;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "服务器端口", "服务器名", "节点名", "jdbc连接池名" };
        return new String[] { "ServerPort", "ServerName", "NodeName", "jdbcPoolName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "创建数", "关闭数", "jdbc池大小", "空闲大小", "等待线程数", "使用率", "使用时间", "空闲时间" };
        //return new String[] { "CreationNum", "CloseNum", "JDBCPoolSize", "FreeSize", "WaitThreadsNum", "Usage", "UseTime", "FreeTime" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8" };
    }
}
